﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.AspNetCore.Http;
using Senparc.Weixin.MP.AdvancedAPIs;
using Senparc.Weixin.MP.TenPayLibV3;
namespace FwProject.PublicService.WxPay
{
   public class WxPayV3
    {
        public delegate void NotifySuccess(NotyfyResult e);
        public delegate void NotifyFail(NotyfyResult e);

        /// <summary>
        /// 回调处理
        /// </summary>
        /// <param name="wxKey">微信支付授权KEY</param>
        /// <param name="success"></param>
        /// <param name="fail"></param>
        /// <returns></returns>
        public static string ProcessNotify(string wxKey, NotifySuccess success, NotifyFail fail, HttpContext httpContext)
        {
            NotyfyResult result = new NotyfyResult();
            ResponseHandler resHandler = new ResponseHandler(httpContext);
            try
            {
                result.Content = resHandler.ParseXML();
                string openid = resHandler.GetParameter("openid");
                string out_trade_no = resHandler.GetParameter("out_trade_no");
                string transaction_id = resHandler.GetParameter("transaction_id");
                string total_fee = resHandler.GetParameter("total_fee");
                result = new NotyfyResult()
                {
                    Content = resHandler.ParseXML(),
                    out_trade_no = out_trade_no,
                    openid = openid,
                    transaction_id = transaction_id,
                    total_fee = total_fee,
                    appid = resHandler.GetParameter("appid"),
                    fee_type = resHandler.GetParameter("fee_type"),
                    is_subscribe = resHandler.GetParameter("is_subscribe"),
                    mch_id = resHandler.GetParameter("mch_id"),
                    result_code = resHandler.GetParameter("result_code"),
                    time_end = resHandler.GetParameter("time_end"),
                };
                resHandler.SetKey(wxKey);
                bool signResult = resHandler.IsTenpaySign();
                if (signResult)
                {
                    success(result);
                    return "success";
                }
                else
                {
                    fail(result);
                    return "error";
                }
            }
            catch (Exception ex)
            {
                result.Content = ex.Message;
                fail(result);
                return "error";
            }

        }
        public class NotyfyResult
        {
            /// <summary>
            /// 完成的回调返回内容
            /// </summary>
            public string Content { set; get; }
            public string openid { set; get; }
            public string transaction_id { set; get; }
            public string total_fee { set; get; }
            public string appid { set; get; }
            public string fee_type { set; get; }
            public string is_subscribe { set; get; }
            public string mch_id { set; get; }
            public string out_trade_no { set; get; }
            public string result_code { set; get; }
            public string time_end { set; get; }
        }
    }
    /// <summary>
    /// 微信通知参数
    /// </summary>
  

}
